package com.ptswitch.material.web.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;

import com.ptswitch.material.model.Bom;
import com.ptswitch.material.model.Material;
import com.ptswitch.material.model.Upload;
import com.ptswitch.material.util.Constant;

@Controller("zipUploadAction")
public class ZipUploadAction extends UploadAction {

	public String toImportZip() {
		mustAdmin();
		return "import";
	}

	public String importZip() {
		mustAdmin();
		uploads();
		if (uploads.isEmpty()) {
			return "import";
		}
		Upload upload = uploads.get(0);
		List<Material> materials = new ArrayList<Material>();
		List<Bom> boms = new ArrayList<Bom>();
		excelSer.buildDataFromZip(materials, boms, ActionSupport.getSavePath(), upload.getFilePath());
		materialSer.saveAll(materials);
		for (Bom bom : boms) {
			bomSer.saveEx(bom);
		}
		hlogSer.log(visitor(), Constant.updateSystem);
		return "index";
	}

	public String exportZip() {
		excelSer.outputDataToZip(materialSer.getAll(), bomSer.getAll(), ActionSupport.getSavePath(),
				"output/export.zip");
		this.upload = new Upload();
		this.upload.setFilePath("output/export.zip");
		this.upload.setFileName("export.zip");
		return "download";
	}

	// get and set

}
